if(CMAKE_VERSION VERSION_LESS @MITK_CMAKE_MINIMUM_REQUIRED_VERSION@)
  message(FATAL_ERROR "MITK requires at least CMake Version @MITK_CMAKE_MINIMUM_REQUIRED_VERSION@")
endif()

# The MITK version number
set(MITK_VERSION_MAJOR "@MITK_VERSION_MAJOR@")
set(MITK_VERSION_MINOR "@MITK_VERSION_MINOR@")
set(MITK_VERSION_PATCH "@MITK_VERSION_PATCH@")
set(MITK_VERSION_STRING "@MITK_VERSION_STRING@")

#-----------------------------------------------------------------------------
# C++ language standard
#-----------------------------------------------------------------------------

set(MITK_CXX_STANDARD @MITK_CXX_STANDARD@)

#-----------------------------------------------------------------------------
# Include required CMake scripts
#-----------------------------------------------------------------------------

# Update the CMake module path
set(MITK_CMAKE_MODULE_PATH "@MITK_SOURCE_DIR@/CMake")
list(APPEND CMAKE_MODULE_PATH ${MITK_CMAKE_MODULE_PATH})

# Standard CMake macros
include(CMakeParseArguments)
include(FeatureSummary)
include(FindPackageHandleStandardArgs)
include(GenerateExportHeader)

# Include MITK macros
include(MacroParseArguments)
include(mitkFunctionAddCustomModuleTest)
include(mitkFunctionCheckMitkCompatibility)
include(mitkFunctionCheckModuleDependencies)
include(mitkFunctionConfigureVisualStudioUserProjectFile)
include(mitkFunctionCreateBlueBerryApplication)
include(mitkFunctionCreateCommandLineApp)
include(mitkFunctionCreateModule)
include(mitkFunctionCreatePlugin)
include(mitkFunctionCreateProvisioningFile)
include(mitkFunctionCreateWindowsBatchScript)
include(mitkFunctionGetLibrarySearchPaths)
include(mitkFunctionInstallAutoLoadModules)
include(mitkFunctionInstallCTKPlugin)
include(mitkFunctionInstallProvisioningFiles)
include(mitkFunctionInstallThirdPartyCTKPlugins)
include(mitkFunctionOrganizeSources)
include(mitkFunctionUseModules)
include(mitkMacroCreateExecutable)
include(mitkMacroCreateModuleTests)
include(mitkMacroFindDependency)
include(mitkMacroGenerateToolsLibrary)
include(mitkMacroGetPMDPlatformString)
include(mitkMacroInstall)
include(mitkMacroInstallHelperApp)
include(mitkMacroInstallTargets)
include(mitkMacroMultiplexPicType)

#-----------------------------------------------------------------------------
# MITK flags and directories
#-----------------------------------------------------------------------------

# MITK compiler flags
set(MITK_C_FLAGS "@MITK_C_FLAGS@")
set(MTTK_C_FLAGS_DEBUG "@MITK_C_FLAGS_DEBUG@")
set(MITK_C_FLAGS_RELEASE "@MITK_C_FLAGS_RELEASE@")
set(MITK_CXX_FLAGS "@MITK_CXX_FLAGS@")
set(MTTK_CXX_FLAGS_DEBUG "@MITK_CXX_FLAGS_DEBUG@")
set(MITK_CXX_FLAGS_RELEASE "@MITK_CXX_FLAGS_RELEASE@")

# MITK linker flags
set(MITK_EXE_LINKER_FLAGS "@MITK_EXE_LINKER_FLAGS@")
set(MITK_SHARED_LINKER_FLAGS "@MITK_SHARED_LINKER_FLAGS@")
set(MITK_MODULE_LINKER_FLAGS "@MITK_MODULE_LINKER_FLAGS@")

# MITK specific directories
set(MITK_SOURCE_DIR "@MITK_SOURCE_DIR@")
set(MITK_BINARY_DIR "@MITK_BINARY_DIR@")
set(MITK_CMAKE_DIR "@MITK_CMAKE_DIR@")

# MITK output directories
set(MITK_CMAKE_RUNTIME_OUTPUT_DIRECTORY "@MITK_CMAKE_RUNTIME_OUTPUT_DIRECTORY@")
set(MITK_CMAKE_ARCHIVE_OUTPUT_DIRECTORY "@MITK_CMAKE_ARCHIVE_OUTPUT_DIRECTORY@")
set(MITK_CMAKE_LIBRARY_OUTPUT_DIRECTORY "@MITK_CMAKE_LIBRARY_OUTPUT_DIRECTORY@")

#-----------------------------------------------------------------------------
# Miscellaneous variables
#-----------------------------------------------------------------------------

# Internal version numbers, used for approximate compatibility checks
# of a MITK development version (non-release).
set(MITK_VERSION_PLUGIN_SYSTEM 2) # dropped legacy BlueBerry plug-in CMake support

set(MITK_DATA_DIR "@MITK_DATA_DIR@")
set(UTILITIES_DIR "@UTILITIES_DIR@")
set(REGISTER_QFUNCTIONALITY_CPP_IN "@REGISTER_QFUNCTIONALITY_CPP_IN@")
set(MITK_DOXYGEN_TAGFILE_NAME "@MITK_DOXYGEN_TAGFILE_NAME@")
set(MITK_LEGACY_EXPORT_MACRO_NAME 1)

set(DCMTK_CMAKE_DEBUG_POSTFIX @DCMTK_CMAKE_DEBUG_POSTFIX@)

# Get the canonical name of the directory to avoid potential case mismatch,
# e.g. in the drive letter on Windows.
get_filename_component(CMAKE_CURRENT_LIST_DIR_REALPATH ${CMAKE_CURRENT_LIST_DIR} REALPATH)
if(CMAKE_CURRENT_LIST_DIR_REALPATH STREQUAL MITK_BINARY_DIR)
  set(MITK_EXTERNAL_PROJECT_PREFIX @MITK_EXTERNAL_PROJECT_PREFIX@)
endif()

set(MITK_MODULES_PACKAGE_DEPENDS_DIR "@MITK_MODULES_PACKAGE_DEPENDS_DIR@")
if(MODULES_PACKAGE_DEPENDS_DIRS)
  list(APPEND MODULES_PACKAGE_DEPENDS_DIRS ${MITK_MODULES_PACKAGE_DEPENDS_DIR})
  list(REMOVE_DUPLICATES MODULES_PACKAGE_DEPENDS_DIRS)
else()
  set(MODULES_PACKAGE_DEPENDS_DIRS ${MITK_MODULES_PACKAGE_DEPENDS_DIR})
endif()

#-----------------------------------------------------------------------------
# External dependencies
#-----------------------------------------------------------------------------

list(APPEND CMAKE_PREFIX_PATH "@MITK_EXTERNAL_PROJECT_PREFIX@")

# -----------------------------------------
# Qt variables and dependencies

set(MITK_USE_Qt5 @MITK_USE_Qt5@)

if(MITK_USE_Qt5)
  set(MITK_QT5_MINIMUM_VERSION @MITK_QT5_MINIMUM_VERSION@)
  set(MITK_QT5_COMPONENTS @MITK_QT5_COMPONENTS@)
  mitkMacroFindDependency(Qt5 ${MITK_QT5_MINIMUM_VERSION} COMPONENTS ${MITK_QT5_COMPONENTS})
endif()

# -----------------------------------------
# Special Python variables

set(MITK_USE_Python3 @MITK_USE_Python3@)
if(MITK_USE_Python3)
  set(PYTHON_EXECUTABLE "@PYTHON_EXECUTABLE@" CACHE FILEPATH "")
  set(PYTHON_INCLUDE_DIR "@PYTHON_INCLUDE_DIR@" CACHE PATH "")
  set(PYTHON_LIBRARY "@PYTHON_LIBRARY@" CACHE FILEPATH "")
  set(PYTHON_INCLUDE_DIR2 "@PYTHON_INCLUDE_DIR2@" CACHE PATH "")

  mitkMacroFindDependency(Python3 COMPONENTS Interpreter Development NumPy)
endif()

# -----------------------------------------
# Special Boost variables

set(MITK_USE_Boost_LIBRARIES @MITK_USE_Boost_LIBRARIES@)
set(MITK_USE_SYSTEM_Boost @MITK_USE_SYSTEM_Boost@)
set(BOOST_ROOT "@BOOST_ROOT@" CACHE PATH "")
set(BOOST_LIBRARYDIR "@BOOST_LIBRARYDIR@" CACHE PATH "")
set(Boost_ADDITIONAL_VERSIONS 1.68 1.68.0)

# We need this later for a DCMTK workaround
set(_dcmtk_dir_orig "@DCMTK_DIR@")

# -----------------------------------------
# External dependencies from the superbuild
# or injected from somewhere else via
# <project>_DIR variables.

@MITK_CONFIG_EXTERNAL_PROJECTS@

# Ensure the MITK module path comes first
set(CMAKE_MODULE_PATH ${MITK_CMAKE_MODULE_PATH} ${CMAKE_MODULE_PATH})

# -----------------------------------------
# Special handling for DCMTK

if(MITK_USE_DCMTK)
  # Due to the preferred CONFIG mode in find_package calls above,
  # the DCMTKConfig.cmake file is read, which does not provide useful
  # package information. We explictly need MODULE mode to find DCMTK.
  if(${_dcmtk_dir_orig} MATCHES "${MITK_EXTERNAL_PROJECT_PREFIX}.*")
    # Help our FindDCMTK.cmake script find our super-build DCMTK
    set(DCMTK_DIR ${MITK_EXTERNAL_PROJECT_PREFIX})
  else()
    # Use the original value
    set(DCMTK_DIR ${_dcmtk_dir_orig})
  endif()
  find_package(DCMTK REQUIRED MODULE)
endif()

# -----------------------------------------
# Special handling for DCMQI
if(MITK_USE_DCMQI)
  # Due to the preferred CONFIG mode in find_package calls above,
  # the DCMQIConfig.cmake file is read, which does not provide useful
  # package information. We explictly need MODULE mode to find DCMQI.
    # Help our FindDCMQI.cmake script find our super-build DCMQI
  set(DCMQI_DIR ${MITK_EXTERNAL_PROJECT_PREFIX})
  find_package(DCMQI REQUIRED)
endif()

# -----------------------------------------
# Special handling for Boost

if(MITK_USE_Boost)
  link_directories(${Boost_LIBRARY_DIRS})
endif()

# -----------------------------------------
# Special handling for OpenIGTLink

if(MITK_USE_OpenIGTLink)
  link_directories(${OpenIGTLink_LIBRARY_DIRS})
endif()

# -----------------------------------------
# Internal project dependencies

set(CppMicroServices_DIR "@CppMicroServices_DIR@")
mitkMacroFindDependency(CppMicroServices)

set(MITK_USE_BLUEBERRY @MITK_USE_BLUEBERRY@)
if(MITK_USE_BLUEBERRY)

  set(MITK_PLUGIN_USE_FILE "@MITK_PLUGIN_USE_FILE@")
  if(MITK_PLUGIN_USE_FILE)
    if(EXISTS "${MITK_PLUGIN_USE_FILE}")
      include("${MITK_PLUGIN_USE_FILE}")
    endif()
  endif()

  set(MITK_PLUGIN_PROVISIONING_FILE "@MITK_EXTAPP_PROVISIONING_FILE@")
  set(MITK_PROVISIONING_FILES
      "${BLUEBERRY_PLUGIN_PROVISIONING_FILE}"
      "${MITK_PLUGIN_PROVISIONING_FILE}")

endif()

set(BLUEBERRY_USE_QT_HELP @BLUEBERRY_USE_QT_HELP@)
if(BLUEBERRY_USE_QT_HELP AND DOXYGEN_VERSION VERSION_LESS "1.8.7")
  message("Setting BLUEBERRY_USE_QT_HELP to OFF because Doxygen version 1.8.7 or newer not found.")
  set(BLUEBERRY_USE_QT_HELP OFF)
endif()

set(BLUEBERRY_QTPLUGIN_PATH "@BLUEBERRY_QTPLUGIN_PATH@")

set(QT_HELPGENERATOR_EXECUTABLE "@QT_HELPGENERATOR_EXECUTABLE@")
set(QT_COLLECTIONGENERATOR_EXECUTABLE "@QT_COLLECTIONGENERATOR_EXECUTABLE@")
set(QT_ASSISTANT_EXECUTABLE "@QT_ASSISTANT_EXECUTABLE@")
set(QT_XMLPATTERNS_EXECUTABLE "@QT_XMLPATTERNS_EXECUTABLE@")


#-----------------------------------------------------------------------------
# MITK sub-project variables
#-----------------------------------------------------------------------------

# External SDK directories
set(MITK_PMD_SDK_DIR @MITK_PMD_SDK_DIR@)

# MITK ToF use variables

set(MITK_TOF_PMDCAMCUBE_AVAILABLE @MITK_USE_TOF_PMDCAMCUBE@)
if(MITK_TOF_PMDCAMCUBE_AVAILABLE AND NOT ${PROJECT_NAME} STREQUAL "MITK")
  option(MITK_USE_TOF_PMDCAMCUBE "Enable support for PMD Cam Cube" @MITK_USE_TOF_PMDCAMCUBE@)
  mark_as_advanced(MITK_USE_TOF_PMDCAMCUBE)
endif()

set(MITK_TOF_PMDCAMBOARD_AVAILABLE @MITK_USE_TOF_PMDCAMBOARD@)
if(MITK_TOF_PMDCAMBOARD_AVAILABLE AND NOT ${PROJECT_NAME} STREQUAL "MITK")
  option(MITK_USE_TOF_PMDCAMBOARD "Enable support for PMD Cam Board" @MITK_USE_TOF_PMDCAMBOARD@)
  mark_as_advanced(MITK_USE_TOF_PMDCAMBOARD)
endif()

set(MITK_TOF_PMDO3_AVAILABLE @MITK_USE_TOF_PMDO3@)
if(MITK_TOF_PMDO3_AVAILABLE AND NOT ${PROJECT_NAME} STREQUAL "MITK")
  option(MITK_USE_TOF_PMDO3 "Enable support for PMD =3" @MITK_USE_TOF_PMDO3@)
  mark_as_advanced(MITK_USE_TOF_PMDO3)
endif()

set(MITK_TOF_KINECT_AVAILABLE @MITK_USE_TOF_KINECT@)
if(MITK_TOF_KINECT_AVAILABLE AND NOT ${PROJECT_NAME} STREQUAL "MITK")
  option(MITK_USE_TOF_KINECT "Enable support for Kinect" @MITK_USE_TOF_KINECT@)
  mark_as_advanced(MITK_USE_TOF_KINECT)
endif()

set(MITK_TOF_MESASR4000_AVAILABLE @MITK_USE_TOF_MESASR4000@)
if(MITK_TOF_MESASR4000_AVAILABLE AND NOT ${PROJECT_NAME} STREQUAL "MITK")
  option(MITK_USE_TOF_MESASR4000 "Enable support for MESA SR4000" @MITK_USE_TOF_MESASR4000@)
  mark_as_advanced(MITK_USE_TOF_MESASR4000)
endif()

if(MITK_USE_IGT)
  #include("${MITK_DIR}/mitkIGTConfig.cmake")
endif()

#-----------------------------------------------------------------------------
# Import MITK targets and set custom properties
#-----------------------------------------------------------------------------

if(NOT MITK_EXPORTS_FILE_INCLUDED)
  if(EXISTS "@MITK_EXPORTS_FILE@")
    set(MITK_EXPORTS_FILE_INCLUDED 1)
    include("@MITK_EXPORTS_FILE@")
  endif()
endif()

# Set properties on exported targets
@MITK_EXPORTED_TARGET_PROPERTIES@

#-----------------------------------------------------------------------------
# Install rules
#-----------------------------------------------------------------------------

# Install rules for ToF libraries loaded at runtime
if(EXISTS "@MITK_BINARY_DIR@/mitkToFHardwareInstallRules.cmake")
  include("@MITK_BINARY_DIR@/mitkToFHardwareInstallRules.cmake")
endif()
